<?php

class Products extends Database
{
    protected $id;
	protected $productname;
	protected $amount;
	protected $price;
    protected $category_id;
    protected $imageurl;
	
	function Products($id,$productname,$amount,$price,$category_id,$imageurl)
	{
        parent::__construct();
        $this->id=$id;
        $this->productname=$productname;
		$this->amount=$amount;
		$this->price=$price;
        $this->category_id=$category_id;
        $this->imageurl=$imageurl;
	}
	
	function printProductsGuest($res)
	{
		print("
		<p align=center>
		<table align=center border=1 cellpadding=5 cellspacing=0>
		 <tr bgcolor=#EEEEEE>
		  <td width=500><b>Назва товару</b></td>
		  <td width=100><b>Кількість</b></td>	
		  <td width=100><b>Ціна</b></td>			  	    
		 </tr>
		</table>
		");	
		while ($row = mysql_fetch_array($res))
		{
			print("<br>
			<table align=center border=1 cellpadding=5 cellspacing=0>	
			<tr>
			");
			print("
			 <td width=500 height=50>
			 ".$row[1]."
			 </td>
			 <td width=100>
			 ".$row[2]."
			 </td>
			 <td width=100>
			 ".$row[3]."
			 </td>
			 ");
			print("</tr>");
			print("</table>");	
		}
		print("<p align=center><br>");				
	}
	
	function printProductsAdmin($res)
	{
		print("
		<p align=center>
		<table align=center border=1 cellpadding=5 cellspacing=0>
		 <tr bgcolor=#EEEEEE>
		  <td width=500 colspan=4><b>Назва товару</b></td>
		  <td width=100><b>Кількість</b></td>	
		  <td width=100><b>Ціна</b></td>			  	    
		 </tr>
		</table>
		");		
		while ($row = mysql_fetch_array($res))
		{
			print("<br>
			<table align=center border=1 cellpadding=5 cellspacing=0>	
			<tr>
			");
			print("
			 <td width=500 height=50 colspan=5>
			 	<a href=/index.php?page=product&product=".$row[0]." id=categorylink>".$row[1]."</a>
			 </td>
			 <td width=100 rowspan=2>
			 	".$row[2]."
			 </td>
			 <td width=100 rowspan=2>
			 	".$row[3]."
			 </td>
			 ");
			print("</tr>");
			print("<tr>");
			print("
			 <td height=15 width=90>
			 	<a href=/index.php?page=product_plus&product=".$row[0].">Додати</a>
			 </td>
			 <td height=15 width=90>
			 	<a href=/index.php?page=product_send&product=".$row[0].">Відправити</a>
			 </td>	
			 <td height=15 width=90>
			 	<a href=/index.php?page=product_free&product=".$row[0].">Списати</a>
			 </td>				 		
			 <td height=15 width=90>
			 	<a href=/index.php?page=product_edit&product=".$row[0].">Редагувати</a>
			 </td>
			 <td height=15 width=90>
			 	<a href=/index.php?page=product_delete&product=".$row[0].">Видалити</a>
			 </td>
			 ");
			print("</tr>");
			print("</table>");
		}	
		print("<p align=center><br>");				
	}

	function show()
	{	
		$category=$_GET['category'];
		if(!isset($_POST['sorting']))
		{
    		$sort = 1;
		}
		else
		{
			$sort = $_POST['sorting'];
		}
		if($sort==1)
		{
			$res = mysql_query("SELECT id, productname, amount, price FROM products WHERE category_id='$category' ORDER BY productname");
		}
		if($sort==2)
		{
			$res = mysql_query("SELECT id, productname, amount, price FROM products WHERE category_id='$category' ORDER BY productname DESC");
		}
		if($sort==3)
		{
			$res = mysql_query("SELECT id, productname, amount, price FROM products WHERE category_id='$category' ORDER BY price");
		}
		if($sort==4)
		{
			$res = mysql_query("SELECT id, productname, amount, price FROM products WHERE category_id='$category' ORDER BY price DESC");
		}
		if ($_SESSION['login']=="admin") 
		{
			$this->printProductsAdmin($res);
		}
		else
		{
			$this->printProductsGuest($res);
		}
	}

    function showProduct()
    {
        $product=$_GET['product'];
        $res = mysql_query("SELECT id, productname, amount, price, category_id FROM products WHERE id='$product'");
        print("
		<p align=center>
		<table align=center border=1 cellpadding=5 cellspacing=0>
		 <tr bgcolor=#EEEEEE>
		  <td width=500 colspan=5><b>Назва товару</b></td>
		  <td width=100><b>Кількість</b></td>
		  <td width=100><b>Ціна</b></td>
		 </tr>
		");
        $row = mysql_fetch_array($res);
        print("
			<tr>
			");
        print("
			 <td width=500 height=50 colspan=5>
			 	<a href=/index.php?page=product&product=".$row[0]." id=categorylink>".$row[1]."</a>
			 </td>
			 <td width=100 rowspan=2>
			 	".$row[2]."
			 </td>
			 <td width=100 rowspan=2>
			 	".$row[3]."
			 </td>
			 ");
        print("</tr>");
        print("<tr>");
        print("
			 <td height=15 width=90>
			 	<a href=/index.php?page=product_plus&product=".$row[0].">Додати</a>
			 </td>
			 <td height=15 width=90>
			 	<a href=/index.php?page=product_send&product=".$row[0].">Відправити</a>
			 </td>
			 <td height=15 width=90>
			 	<a href=/index.php?page=product_free&product=".$row[0].">Списати</a>
			 </td>
			 <td height=15 width=90>
			 	<a href=/index.php?page=product_edit&product=".$row[0].">Редагувати</a>
			 </td>
			 <td height=15 width=90>
			 	<a href=/index.php?page=product_delete&product=".$row[0].">Видалити</a>
			 </td>
			 ");
        print("</tr>");
        print("</table>");
    }

    function showImage()
    {
        $product=$_GET['product'];
        $url=mysql_query("SELECT imageurl FROM products WHERE id='$product'");
        $url=mysql_fetch_array($url);
        if($url[0]!="0")
        {
            print("<img src=".$url[0]." width=200 height=200>");
        }
    }

	function search($search)
	{	
		$res = mysql_query("SELECT id, productname, amount, price FROM products WHERE productname LIKE '%$search%'");
		if(mysql_fetch_assoc($res)==NULL)
		{
			print("<br>Такого товару не знайдено!");
		}
		else
		{
			if ($_SESSION['login']=="admin") 
			{
				$this->printProductsAdmin($res);
			}
			else
			{
				$this->printProductsGuest($res);
			}
		}
	}

    function printProductName()
    {
        $sql=mysql_query("SELECT productname FROM products WHERE id='$this->id'");
        $sql=mysql_fetch_array($sql);
        return $sql[0];
    }

    function existingAdd()
    {
        $name=mysql_real_escape_string($this->productname);
        $sql=mysql_query("SELECT COUNT(*) FROM products WHERE productname= '$name' AND category_id='$this->category_id'");
        $sql=mysql_fetch_array($sql);
        if($sql[0]!=0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    function existingEdit()
    {
        $name=mysql_real_escape_string($this->productname);
        $sql=mysql_query("SELECT COUNT(*) FROM products WHERE productname= '$name' AND id!='$this->id' AND category_id='$this->category_id'");
        $sql=mysql_fetch_array($sql);
        if($sql[0]!=0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    function returnAmount()
    {
        $sql=mysql_query("SELECT amount FROM products WHERE id = '$this->id'");
        $sql=mysql_fetch_array($sql);
        return $sql[0];
    }

    function printCategoryID()
    {
        $sql=mysql_query("SELECT category_id FROM products WHERE id = '$this->id'");
        $sql=mysql_fetch_array($sql);
        return $sql[0];
    }

    function add()
    {
        mysql_query("INSERT INTO products (`productname`,amount,price,`category_id`,imageurl) VALUES ('$this->productname',0,'$this->price','$this->category_id',0)");
    }

    function returnProductID()
    {
        $sql=mysql_query("SELECT id FROM products WHERE productname='$this->productname'");
        $sql=mysql_fetch_array($sql);
        return ($sql[0]);
    }

    function addImage($file,$format)
    {
        move_uploaded_file($file, "/home/storage.com/www/images/products/".$this->returnProductID().$format);
        $url="/images/products/".$this->returnProductID().$format;
        $id=$this->returnProductID();
        mysql_query("UPDATE products SET `imageurl`='$url' WHERE id='$id'");
    }

    function delete()
    {
        $url=mysql_query("SELECT imageurl FROM products WHERE id='$this->id'");
        $url=mysql_fetch_array($url);
        unlink(".$url[0].");
        mysql_query("DELETE FROM packaging WHERE product_id='$this->id'");
        mysql_query("DELETE FROM products WHERE id='$this->id'");
    }

}

?>